package com.todod.base.module.system.api.permission;

import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.module.system.dal.dataobject.permission.RoleDO;
import com.todod.base.module.system.service.permission.PermissionService;
import com.todod.base.module.system.service.permission.RoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.todod.base.framework.common.pojo.CommonResult.success;
import static com.todod.base.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class RoleApiImpl implements RoleApi {

    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;


    @Override
    public CommonResult<Boolean> validRoleList(Collection<Long> ids) {
        roleService.validateRoleList(ids);
        return success(true);
    }

    @Override
    public CommonResult<Set<String>> getByUserId(Long userId) {
        List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(userId));
        Set<String> codes = new HashSet<>();
        userRoles.forEach((item) -> {
            codes.add(item.getCode());
        });
        return success(codes);
    }
}
